
<!DOCTYPE HTML>
<html lang="zh-hans" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>最大子序和 · Leet学习笔记</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        <meta name="author" content="黄文庆">
        
        
    
    <link rel="stylesheet" href="../gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-code/plugin.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-search-plus/search.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-expandable-chapters/expandable-chapters.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-splitter/splitter.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-katex/katex.min.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-search/search.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-fontsettings/website.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-theme-comscore/test.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">

    
    
    <link rel="prev" href="中位数.html" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="输入并搜索" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="../">
            
                <a href="../">
            
                    
                    Introduction
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="Chapter1.html">
            
                <a href="Chapter1.html">
            
                    
                    分治法
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.2.1" data-path="中位数.html">
            
                <a href="中位数.html">
            
                    
                    中位数
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="1.2.2" data-path="最大子序和.html">
            
                <a href="最大子序和.html">
            
                    
                    最大子序和
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            本书使用 GitBook 发布
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href=".." >最大子序和</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div class="search-plus" id="book-search-results">
    <div class="search-noresults">
    
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <h1 id="&#x6700;&#x5927;&#x5B50;&#x5E8F;&#x548C;">&#x6700;&#x5927;&#x5B50;&#x5E8F;&#x548C;</h1>
<p>&#x7ED9;&#x5B9A;&#x4E00;&#x4E2A;&#x6574;&#x6570;&#x6570;&#x7EC4;nums&#xFF0C;&#x627E;&#x5230;&#x4E00;&#x4E2A;&#x5177;&#x6709;&#x6700;&#x5927;&#x548C;&#x7684;&#x8FDE;&#x7EED;&#x5B50;&#x6570;&#x7EC4;(&#x5B50;&#x6570;&#x7EC4;&#x6700;&#x5C11;&#x5305;&#x542B;&#x4E00;&#x4E2A;&#x5143;&#x7D20;)&#xFF0C;&#x8FD4;&#x56DE;&#x5176;&#x6700;&#x5927;&#x548C;&#x3002;<br><strong> &#x793A;&#x4F8B; </strong>:<br><em>&#x8F93;&#x5165;</em>: [-2,1,-3,4,-1,2,1,-5,4]<br><em>&#x8F93;&#x51FA;</em>: <code>6</code><br><em>&#x89E3;&#x91CA;</em>: &#x8FDE;&#x7EED;&#x5B50;&#x6570;&#x7EC4; [4,-1,2,1] &#x7684;&#x548C;&#x6700;&#x5927;&#x4E3A;<code>6</code></p>
<p><strong>&#x65B9;&#x6CD5;&#xFF1A; &#x5206;&#x6CBB;&#x6CD5; </strong></p>
<p><strong>&#x7B97;&#x6CD5;&#x5206;&#x6790;&#xFF1A;</strong></p>
<ol>
<li>&#x628A;&#x6570;&#x7EC4;&#x5206;&#x6210;&#x5DE6;&#x53F3;&#x4E24;&#x4E2A;&#x5B50;&#x6570;&#x7EC4;&#xFF0C;&#x6700;&#x5927;&#x81EA;&#x5E8F;&#x548C;&#x53EA;&#x53EF;&#x80FD;&#x51FA;&#x73B0;&#x5728;<br>&#x3000;1.&#x5DE6;&#x5B50;&#x6570;&#x7EC4;<br>&#x3000;2.&#x53F3;&#x5B50;&#x6570;&#x7EC4;<br>&#x3000;3.&#x6A2A;&#x8DE8;&#x5DE6;&#x53F3;&#x5B50;&#x6570;&#x7EC4;&#x7684;&#x90E8;&#x5206;&#x6216;&#x5168;&#x90E8;&#x5143;&#x7D20;   </li>
<li>&#x7136;&#x540E;&#x5BF9;&#x5DE6;&#x5B50;&#x6570;&#x7EC4;&#x6216;&#x53F3;&#x5B50;&#x6570;&#x7EC4;&#x65F6;&#xFF0C;&#x8FDB;&#x4E00;&#x6B65;&#x62C6;&#x5206;&#xFF0C;&#x4F9D;&#x6B21;&#x5FAA;&#x73AF;&#xFF0C;&#x76F4;&#x81F3;&#x62C6;&#x5206;&#x7684;&#x5B50;&#x6570;&#x7EC4;&#x4E2D;&#x53EA;&#x6709;&#x4E00;&#x4E2A;&#x5143;&#x7D20;&#x3002;<ul>
<li>&#x62C6;&#x5206;&#x5E8F;&#x5217;&#xFF08;&#x76F4;&#x5230;&#x53EA;&#x5269;&#x4E0B;&#x4E00;&#x4E2A;&#x6570;&#x7684;&#x6570;&#x7EC4;&#xFF09;</li>
<li>&#x6C42;&#x5DE6;&#x5B50;&#x6570;&#x7EC4;&#x6700;&#x5927;&#x503C;</li>
<li>&#x6C42;&#x53F3;&#x5B50;&#x6570;&#x7EC4;&#x6700;&#x5927;&#x503C;</li>
<li>&#x6C42;&#x6A2A;&#x8DE8;&#x5DE6;&#x53F3;&#x5B50;&#x6570;&#x7EC4;&#x7684;&#x6700;&#x5927;&#x503C;</li>
</ul>
</li>
<li>&#x5408;&#x5E76;&#xFF0C;&#x5F97;&#x51FA;&#x4EE5;&#x4E0A;&#x4E09;&#x4E2A;&#x6700;&#x5927;&#x503C;&#x7684;&#x6700;&#x5927;&#x503C;</li>
<li>&#x5F53;&#x6700;&#x5927;&#x5B50;&#x6570;&#x7EC4;&#x6709; <span class="katex"><span class="katex-mathml"><math><semantics><mrow><mi>n</mi></mrow><annotation encoding="application/x-tex">n</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="strut" style="height:0.43056em;"></span><span class="strut bottom" style="height:0.43056em;vertical-align:0em;"></span><span class="base textstyle uncramped"><span class="mord mathit">n</span></span></span></span> &#x4E2A;&#x6570;&#x5B57;&#x65F6;&#xFF1A;<ul>
<li>&#x82E5; <span class="katex"><span class="katex-mathml"><math><semantics><mrow><mi>n</mi><mo>=</mo><mo>=</mo><mn>1</mn></mrow><annotation encoding="application/x-tex">n==1</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="strut" style="height:0.64444em;"></span><span class="strut bottom" style="height:0.64444em;vertical-align:0em;"></span><span class="base textstyle uncramped"><span class="mord mathit">n</span><span class="mrel">=</span><span class="mrel">=</span><span class="mord mathrm">1</span></span></span></span>&#xFF0C;&#x8FD4;&#x56DE;&#x6B64;&#x5143;&#x7D20;&#x3002;</li>
<li><em>left_sum</em> &#x662F;&#x5DE6;&#x5B50;&#x6570;&#x7EC4;&#x7684;&#x5143;&#x7D20;&#x4E4B;&#x548C;&#x6700;&#x5927;&#x503C;</li>
<li><em>right_sum</em> &#x662F;&#x53F3;&#x5B50;&#x6570;&#x7EC4;&#x7684;&#x5143;&#x7D20;&#x4E4B;&#x548C;&#x6700;&#x5927;&#x503C;</li>
<li><em>cross_sum</em> &#x662F;&#x6A2A;&#x8DE8;&#x5DE6;&#x53F3;&#x5B50;&#x6570;&#x7EC4;&#x5143;&#x7D20;&#x4E4B;&#x548C;&#x7684;&#x6700;&#x5927;&#x503C;</li>
</ul>
</li>
</ol>
<p><strong>&#x4EE3;&#x7801;&#x5982;&#x4E0B;:</strong></p>
<pre><code class="lang-cpp">    <span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;iostream&gt;</span></span>
    <span class="hljs-keyword">using</span> <span class="hljs-built_in">std</span>::<span class="hljs-built_in">cout</span>;
    <span class="hljs-keyword">using</span> <span class="hljs-built_in">std</span>::<span class="hljs-built_in">cin</span>;
    <span class="hljs-keyword">using</span> <span class="hljs-built_in">std</span>::<span class="hljs-built_in">endl</span>;
    <span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">CrossSum</span><span class="hljs-params">(<span class="hljs-keyword">int</span> nums[],<span class="hljs-keyword">int</span> left, <span class="hljs-keyword">int</span> right, <span class="hljs-keyword">int</span> mid)</span> </span>{
        <span class="hljs-keyword">if</span> (left == right) <span class="hljs-keyword">return</span> nums[left];
        <span class="hljs-keyword">int</span> leftSubSum=<span class="hljs-number">0</span>;
        <span class="hljs-keyword">int</span> leftMaxSum=nums[mid];<span class="hljs-comment">//&#x6A2A;&#x8DE8;&#x5DE6;&#x53F3;&#x5B50;&#x6570;&#x7EC4;&#xFF0C;&#x5219;&#x57FA;&#x51C6;&#x5143;&#x7D20;(&#x5DE6;&#x8FB9;&#x7B2C;&#x4E00;&#x4E2A;&#x5143;&#x7D20;)&#x5FC5;&#x7136;&#x5305;&#x542B;&#x5728;&#x5185;</span>
        <span class="hljs-keyword">for</span>(<span class="hljs-keyword">int</span> i=mid; i&gt;=left; i--) {
            leftSubSum+=nums[i];
            leftMaxSum=leftMaxSum&gt;=leftSubSum?leftMaxSum:leftSubSum;
        }
        <span class="hljs-keyword">int</span> rightSubSum=<span class="hljs-number">0</span>;
        <span class="hljs-keyword">int</span> rightMaxSum=nums[mid+<span class="hljs-number">1</span>];<span class="hljs-comment">//&#x6A2A;&#x8DE8;&#x5DE6;&#x53F3;&#x5B50;&#x6570;&#x7EC4;&#xFF0C;&#x5219;&#x53F3;&#x8FB9;&#x7B2C;&#x4E00;&#x4E2A;&#x5143;&#x7D20;&#x5FC5;&#x7136;&#x5305;&#x542B;&#x5728;&#x5185;</span>
        <span class="hljs-keyword">for</span>(<span class="hljs-keyword">int</span> i=mid+<span class="hljs-number">1</span>; i&lt;=right; i++) {
            rightSubSum+=nums[i];
            rightMaxSum=rightMaxSum&gt;=rightSubSum?rightMaxSum:rightSubSum;
        }
        <span class="hljs-keyword">return</span> leftMaxSum+rightMaxSum;
    }

    <span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">fun</span><span class="hljs-params">(<span class="hljs-keyword">int</span> nums[],<span class="hljs-keyword">int</span> left, <span class="hljs-keyword">int</span> right)</span> </span>{
        <span class="hljs-keyword">if</span> (left == right) <span class="hljs-keyword">return</span> nums[left];

        <span class="hljs-keyword">int</span> mid = (left + right) / <span class="hljs-number">2</span>;
        <span class="hljs-keyword">int</span> leftSum = fun(nums, left, mid);
        <span class="hljs-keyword">int</span> rightSum = fun(nums, mid + <span class="hljs-number">1</span>, right);
        <span class="hljs-keyword">int</span> crossSum = CrossSum(nums, left, right, mid);
        <span class="hljs-keyword">int</span> temp=leftSum&gt;rightSum?leftSum:rightSum;

        <span class="hljs-keyword">return</span> temp&gt;crossSum?temp:crossSum;
    }

    <span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">MaxSubArray</span><span class="hljs-params">(<span class="hljs-keyword">int</span> nums[],<span class="hljs-keyword">int</span> length)</span> </span>{
        <span class="hljs-keyword">return</span> fun(nums,<span class="hljs-number">0</span>,length<span class="hljs-number">-1</span>);
    }

    <span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span> </span>{
        <span class="hljs-keyword">int</span> nums[<span class="hljs-number">9</span>]={<span class="hljs-number">-2</span>,<span class="hljs-number">1</span>,<span class="hljs-number">-3</span>,<span class="hljs-number">4</span>,<span class="hljs-number">-1</span>,<span class="hljs-number">2</span>,<span class="hljs-number">1</span>,<span class="hljs-number">-5</span>,<span class="hljs-number">4</span>};
        <span class="hljs-keyword">int</span> maxSum=MaxSubArray(nums,<span class="hljs-number">9</span>);
        <span class="hljs-built_in">cout</span>&lt;&lt;<span class="hljs-string">&quot;maxSum= &quot;</span>&lt;&lt;maxSum&lt;&lt;<span class="hljs-built_in">endl</span>;

    }
</code></pre>
<p><strong>&#x8FD0;&#x884C;&#x7ED3;&#x679C;&#xFF1A;</strong>  </p>
<pre><code>maxSum= 6
</code></pre><p><strong>&#x7B97;&#x6CD5;&#x590D;&#x6742;&#x5EA6;&#x5206;&#x6790;:</strong>  </p>
<ul>
<li><strong>&#x9012;&#x5F52;&#x6811;&#x6CD5;&#xFF1A;</strong><br>&#x65F6;&#x95F4;&#x590D;&#x6742;&#x5EA6;&#xFF1A;<span class="katex"><span class="katex-mathml"><math><semantics><mrow><mi>O</mi><mo>(</mo><mi>n</mi><mi>l</mi><mi>g</mi><mo>(</mo><mi>n</mi><mo>)</mo><mo>)</mo></mrow><annotation encoding="application/x-tex">O(nlg(n))</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="strut" style="height:0.75em;"></span><span class="strut bottom" style="height:1em;vertical-align:-0.25em;"></span><span class="base textstyle uncramped"><span class="mord mathit" style="margin-right:0.02778em;">O</span><span class="mopen">(</span><span class="mord mathit">n</span><span class="mord mathit" style="margin-right:0.01968em;">l</span><span class="mord mathit" style="margin-right:0.03588em;">g</span><span class="mopen">(</span><span class="mord mathit">n</span><span class="mclose">)</span><span class="mclose">)</span></span></span></span>
<img src="picture/1-1.jpeg" alt="1-1"></li>
</ul>

<script>console.log("plugin-popup....");document.onclick = function(e){ e.target.tagName === "IMG" && window.open(e.target.src,e.target.src)}</script><style>img{cursor:pointer}</style>
                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="中位数.html" class="navigation navigation-prev navigation-unique" aria-label="Previous page: 中位数">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"最大子序和","level":"1.2.2","depth":2,"previous":{"title":"中位数","level":"1.2.1","depth":2,"path":"Chapter1/中位数.md","ref":"Chapter1/中位数.md","articles":[]},"dir":"ltr"},"config":{"plugins":["code","github","theme-comscore","search-plus","expandable-chapters","splitter","popup","katex","livereload"],"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"pluginsConfig":{"github":{"url":"https://github.com/micro-coder/ROS-Note"},"livereload":{},"splitter":{},"search":{},"popup":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"code":{"copyButtons":true},"katex":{},"fontsettings":{"theme":"white","family":"sans","size":2},"highlight":{},"theme-comscore":{},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false,"treeLevel":true},"expandable-chapters":{},"search-plus":{}},"theme":"default","author":"黄文庆","pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"variables":{},"title":"Leet学习笔记","language":"zh-hans","gitbook":"*","description":""},"file":{"path":"Chapter1/最大子序和.md","mtime":"2020-04-30T08:02:43.501Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2020-04-30T08:05:15.505Z"},"basePath":"..","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="../gitbook/gitbook.js"></script>
    <script src="../gitbook/theme.js"></script>
    
        
        <script src="../gitbook/gitbook-plugin-code/plugin.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-github/plugin.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-search-plus/jquery.mark.min.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-search-plus/search.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-expandable-chapters/expandable-chapters.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-splitter/splitter.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-livereload/plugin.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-search/search-engine.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-search/search.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-theme-comscore/test.js"></script>
        
    

    </body>
</html>

